/**
 * Created by xc on 15/12/22.
 */

var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var ItbBaseModel = require('./ItbBaseModel');


// Schema 结构
var ItbRelayInfoModel = function(obj){
    ItbBaseModel.call(this,obj);

  // 以下为表定义数据，需要的字段就填入
  //

};
ItbRelayInfoModel.prototype = new ItbBaseModel();

// 取得表名
ItbRelayInfoModel.prototype.getTableName = function(){
    return 'itb_relay_info';
}
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
ItbRelayInfoModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'id='+this.id;
}

// 根据UnionId查找记录
// 参数：union_d
// 参数：callback 回调，参数如下：
//          error:错误
//          result:多行数据
ItbRelayInfoModel.prototype.findByUnionId = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('ItbRelayInfoModel.findByUnionId Err:'+err);
        }
        // Use the connection
        var sql = 'SELECT * FROM itb_relay_info t ' +
            ' WHERE t.union_id = ? AND t.del_flg = 0 ' +
            ' ORDER BY t.update_time DESC ';
        var sqlValue = [THIS.union_id];
        var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();

            console.log();

            callback(error, result);
        });
    });
}

// 根据文档Id查找记录
// 参数：doc_id
// 参数：callback 回调，参数如下：
//          error:错误
//          result:多行数据
ItbRelayInfoModel.prototype.findByDocId = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('ItbRelayInfoModel.findByDocId Err:'+err);
        }
        // Use the connection
        var sql = 'SELECT * FROM itb_relay_info t ' +
            ' WHERE t.doc_id = ? AND t.del_flg = 0' +
            ' ORDER BY t.update_time DESC ';
        var sqlValue = [THIS.doc_id];
        var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();

            console.log();

            callback(error, result);
        });
    });
}

// 文档转发次数
// 参数：doc_id 文档id
// 参数：period 最近的天数
// 参数：callback 回调
ItbRelayInfoModel.prototype.cntByDocId = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('ItbRelayInfoModel.cntByDocId Err:'+err);
        }
        // Use the connection
        var sql = 'SELECT count(*) as itemCount FROM itb_relay_info t ' +
            ' WHERE t.doc_id = ? AND t.del_flg = 0 ';
        if (THIS.period && THIS.period>0) {
            sql += ' AND to_days(now()) - to_days(t.create_time) < ' + THIS.period;
        }
        var sqlValue = [THIS.doc_id];
        var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();

            var total=0;
            if(error) {
                console.log(error);
            } else if (result) {
                total = result[0].itemCount;
                console.log('find item(s):' + total);
            } else{
                console.log('find no one');
            }
            // 回调
            callback(error, total);
        });
    });
}

// 导出
module.exports = ItbRelayInfoModel;